/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/22
 * Time: 下午2:11
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Map;

import java.io.InputStream;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import com.cake21.uitest.System.Core.Config;

public class CaseMap {

    private static ConcurrentHashMap<String, HashMap<String, String>> map = initMap();

    public static ConcurrentHashMap<String, HashMap<String, String>> getMap() {
        return map;
    }

    private static ConcurrentHashMap<String, HashMap<String, String>> initMap() {
        // 读取XML文件
        //File file = new File(ClassLoader.getSystemResource("case.xml").getFile());
        try {
            // 读取XML文件
            InputStream is = BrowserMap.class.getClassLoader().getResourceAsStream("case.xml");
            NodeList caseList = new Config(is).readTags("case");
            ConcurrentHashMap<String, HashMap<String, String>> caseMap = new ConcurrentHashMap<String, HashMap<String, String>>();
            for (int i = 0; i < caseList.getLength(); i++) {
                // 获取所有用例的全部配置信息
                NamedNodeMap caseAttrs = caseList.item(i).getAttributes();
                HashMap<String, String> attrMap = new HashMap<String, String>();
                attrMap.put("name", Config.getNodeString(caseAttrs.getNamedItem("name")));
                attrMap.put("description", Config.getNodeString(caseAttrs.getNamedItem("description")));
                attrMap.put("script", Config.getNodeString(caseAttrs.getNamedItem("script")));
                attrMap.put("pageLoadTimeout", Config.getNodeString(caseAttrs.getNamedItem("pageLoadTimeout")));
                attrMap.put("scriptTimeout", Config.getNodeString(caseAttrs.getNamedItem("scriptTimeout")));
                attrMap.put("implicitlyWait", Config.getNodeString(caseAttrs.getNamedItem("implicitlyWait")));
                caseMap.put("Case-" + i, attrMap);
            }
            return caseMap;
        } catch (Exception e) {
            return null;
        }
    }
}
